﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.BLL;

public partial class MsgMng_AddModify : System.Web.UI.Page
{
	int id;
	bool modify;

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			tbStartTime.Text = DateTime.Today.ToString();
			tbEndTime.Text = DateTime.Today.AddHours(23).AddMinutes(59).AddSeconds(59).ToString();

			if (Request.QueryString["act"] == "modify" && int.TryParse(Request.QueryString["id"], out id))
			{
				using (var mmb = new MsgMngBLL())
				{
					var msg = mmb.GetMessage(id);
					tbTitle.Text = msg.MessTitle;
					tbContent.Text = msg.MessContent;
					tbStartTime.Text = msg.MessStartTime.ToString();
					tbEndTime.Text = msg.MessEndTime.ToString();
					ddlMessageType.Text = msg.MessageType.MessageTypeNo.ToString();
					if (msg.IsCommon)//公共消息
					{
						rbAllUsers.Checked = true;
						rbSelectUsers.Checked = false;
						fsScope.Visible = false;
					}
					else//个人消息
					{
						rbAllUsers.Checked = false;
						rbSelectUsers.Checked = true;
						fsScope.Visible = true;
						cblUsers.DataSource = mmb.GetReceivers(id);
						cblUsers.DataBind();
						foreach (ListItem item in cblUsers.Items)
							item.Selected = cbCheckAll.Checked;
					}
					fsScope2.Visible = fsScope.Visible;
				}
			}
		}
		if (Request.QueryString["act"] == "modify" && int.TryParse(Request.QueryString["id"], out id))
			modify = true;
	}
	protected void cblBy_SelectedIndexChanged(object sender, EventArgs e)
	{
		var cbl = sender as CheckBoxList;
		ds.Enabled = cbl.Items[0].Selected;
		tbUserId.Enabled = cbl.Items[1].Selected;
		tbUserName.Enabled = cbl.Items[2].Selected;
	}
	protected void rbAllUsers_CheckedChanged(object sender, EventArgs e)
	{
		fsScope.Visible = false;
		fsScope2.Visible = false;
	}
	protected void rbSelectUsers_CheckedChanged(object sender, EventArgs e)
	{
		fsScope.Visible = true;
		fsScope2.Visible = true;
	}
	protected void bSearch_Click(object sender, EventArgs e)
	{
		using (MsgMngBLL mmb = new MsgMngBLL())
		{
			cblUsers.DataSource = mmb.GetFilterUsers(
				cblBy.Items[0].Selected ? (ds.SelectedValue == null ? null : (int?)int.Parse(ds.SelectedValue)) : null,
				cblBy.Items[1].Selected ? tbUserId.Text : null,
				cblBy.Items[2].Selected ? tbUserName.Text : null);
			cblUsers.DataBind();
		}
	}
	protected void cbCheckAll_CheckedChanged(object sender, EventArgs e)
	{
		foreach (ListItem item in cblUsers.Items)
			item.Selected = cbCheckAll.Checked;
	}
	protected void bSaveMsg_Click(object sender, EventArgs e)
	{
		bool isCommon;
		List<int> users = null;
		if (rbAllUsers.Checked) //发公共消息
		{
			isCommon = true;
		}
		else
		{
			isCommon = false;
			users = new List<int>();
			foreach (ListItem item in cblUsers.Items)
			{
				if (item.Selected)
					users.Add(int.Parse(item.Value));
			}
		}
		using (var mmb = new MsgMngBLL())
		{
			if (modify)
				mmb.UpdateMess(id, tbTitle.Text, tbContent.Text, isCommon, int.Parse(ddlMessageType.SelectedValue), DateTime.Parse(tbStartTime.Text), DateTime.Parse(tbEndTime.Text), users);
			else
				mmb.AddNewMessage(tbTitle.Text, tbContent.Text, isCommon, int.Parse(ddlMessageType.SelectedValue), DateTime.Parse(tbStartTime.Text), DateTime.Parse(tbEndTime.Text), ((PBOffice.DAL.User)Session["userManage"]).UserNo, users);
		}
		if (!this.ClientScript.IsStartupScriptRegistered("topost"))
		{
			ClientScript.RegisterStartupScript(this.GetType(), "topost", "document.getElementById('" + bReturn.ClientID + "').click();", true);
		}
	}
}